package com.xudow.app.dynamicstate_old.module.recommend;

import android.content.Intent;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.jude.easyrecyclerview.adapter.BaseViewHolder;
import com.jude.utils.JUtils;
import com.squareup.picasso.Picasso;
import com.xudow.app.R;
import com.xudow.app.XApplication;
import com.xudow.app.dynamicstate_old.data.DynamicModel;
import com.xudow.app.dynamicstate_old.data.ImageModel;
import com.xudow.app.dynamicstate_old.domain.entity.RecommendTalent;
import com.xudow.app.dynamicstate_old.module.user.UserProfileActivity;
import com.xudow.app.dynamicstate_old.widget.SquareImageView;
import com.xudow.app.newui.PreLoginActivity;

import butterknife.BindView;
import butterknife.ButterKnife;
import de.hdodenhof.circleimageview.CircleImageView;

/**
 * Created by ske on 2016/6/11.
 */
public class PreferredTalentViewHolder extends BaseViewHolder<RecommendTalent> {
    @BindView(R.id.image_cover)
    SquareImageView imageCover;
    @BindView(R.id.photo)
    CircleImageView photo;
    @BindView(R.id.name)
    TextView name;
    @BindView(R.id.image_like)
    ImageView imageLike;
    @BindView(R.id.text_like)
    TextView textLike;
    @BindView(R.id.tag)
    TextView tag;

    public PreferredTalentViewHolder(ViewGroup parent) {
        super(parent, R.layout.item_recomment_talent);
        ButterKnife.bind(this, itemView);
    }

    @Override
    public void setData(RecommendTalent data) {
        textLike.setText(data.getLikenum() + "人喜欢");
        name.setText(data.getTalentProfileName());
        name.setOnClickListener(v -> {
            Intent intent = new Intent(getContext(), UserProfileActivity.class);
            intent.putExtra(UserProfileActivity.USER_PROFILE_ID, data.getTalentProfileId());
            getContext().startActivity(intent);
        });
        photo.setOnClickListener(v -> {
            Intent intent = new Intent(getContext(), UserProfileActivity.class);
            intent.putExtra(UserProfileActivity.USER_PROFILE_ID, data.getTalentProfileId());
            getContext().startActivity(intent);
        });
        imageCover.setOnClickListener(v -> {
            Intent intent = new Intent(getContext(), UserProfileActivity.class);
            intent.putExtra(UserProfileActivity.USER_PROFILE_ID, data.getTalentProfileId());
            getContext().startActivity(intent);
        });
        if (data.getLabelList() != null && data.getLabelList().size() != 0)
            tag.setText(data.getLabelList().get(0).getLabelName());
        imageLike.setImageResource(data.getTagLike() == 1 ? R.drawable.talent_liked : R.drawable.talent_like);
        Glide.with(getContext()).load(ImageModel.getXueDouImage(data.getTalentHeadurl())).placeholder(R.drawable.default_head_img).into(photo);
        Glide.with(getContext()).load(ImageModel.getLargeImage(data.getTalentCoverPhoto())).into(imageCover);
        imageLike.setOnClickListener(v -> {
            if (!XApplication.getInstance().isLogin()) {
                getContext().startActivity(new Intent(getContext(), PreLoginActivity.class));
                return;
            }
            if (data.getTagLike() == 1) {//是
                DynamicModel.getInstance().unTagLike(data.getTalentProfileId())
                        .subscribe(message -> {
                            data.setTagLike(0);
                            data.setLikenum(data.getLikenum() - 1);
                            textLike.setText(data.getLikenum() + "人喜欢");
                            imageLike.setImageResource(R.drawable.talent_like);
                            JUtils.Toast("取消标记成功");
                        });
            } else {//0 否
                DynamicModel.getInstance().tagLike(data.getTalentProfileId())
                        .subscribe(message -> {
                            data.setTagLike(1);
                            data.setLikenum(data.getLikenum() + 1);
                            textLike.setText(data.getLikenum() + "人喜欢");
                            imageLike.setImageResource(R.drawable.talent_liked);
                            JUtils.Toast("标记成功");
                        });
            }
        });
    }
}
